<?php
namespace Mobile\Controller;
use Think\Controller;

class IndexController extends Controller{
    
    private $www_url;   //域名
    
    public function _initialize(){
        $this->www_url = C('WWW_ROOT');
    }
    
    /**
     * 云麻豆首页
     * @param type $uid         用户id
     * @param type $p           页码
     * @param type $per_page    单页条目数
     */
    public function index($uid=0, $p=1, $per_page=12){
        //获取单页数据
        $page = page('Weibo', $per_page, array('status' => 1, 'pic_type' => 1), array('add_time' => 'desc'), 'join md_weibo_pic on md_weibo.id=md_weibo_pic.w_id');
        $model = $page['list']; //单页数据
        //数据处理
        if($model != NULL){
            //遍历数据，处理单条微博
            foreach ($model as $key => $value){
                unset($model[$key]);
                $model[$key]['uid'] = $value['uid'];    //用户id
                $model[$key]['wid'] = $value['w_id'];   //微博id
                $model[$key]['content'] = $value['content'];   //微博文本内容
                $model[$key]['show_pic_url'] = $this->www_url . $value['pic_url'];           //原图url
                $model[$key]['avatar_url'] = $this->www_url . get_small_pic($value['pic_url']); //小图url                 
                $model[$key]['is_favour'] = 0;          //用户是否赞过该微博
                if(M('Favour')->where(array('uid' => $uid, 'fid' => $value['w_id'], 'type' => 1))->find()){
                    $model[$key]['is_favour'] = 1;
                }
                $model[$key]['favour_num'] = M('Favour')->where(array('fid' => $value['w_id'], 'type' => 1))->count();
                if($user = M('User')->field('nickname')->where(array('id' => $value['uid']))->find()){
                    $model[$key]['nickname'] = $user['nickname'];
                }
            }
            $data = array(
                "code" => 1, 
                "msg" => "首页信息获取成功", 
                "data" => $model, 
                "total_num" => $page['total']
            );
        }  else {
            $data = array(
                "code" => 0, 
                "msg" => "首页暂无信息"
            );
        }
//        dump($data);
        $this->ajaxReturn($data);
    }
    
   /**
     * 云麻豆首页
     * @param type $id         用户id
     * @param type $p           页码
     * @param type $per_page    单页条目数
     */
    public function nindex($id=0, $p=1, $per_page=12){
        //获取单页数据
        $page = page('User_model', $per_page, array('status' => 1, 'model_card' => array('NEQ','')), 
                array('model_card_updatetime' => 'desc'), '', 'uid, nickname, model_card, favour');
        $model = $page['list']; //单页数据
        if($model != NULL){
            foreach ($model as $key => $value){
                $model[$key]['model_card'] = $this->www_url . $value['model_card'];
                $model[$key]['is_favour'] = 0;          //用户是否赞过该微博
                if(M('Favour')->where(array('uid' => $uid, 'fid' => $value['uid'], 'type' => 1))->find()){
                    $model[$key]['is_favour'] = 1;
                }
            }
            $data = array(
                "code" => 1, 
                "msg" => "首页信息获取成功", 
                "data" => $model, 
                "total_num" => $page['total']
            );
        } else {
            $data = array(
                "code" => 0, 
                "msg" => "首页暂无信息"
            );
        }
        $this->ajaxReturn($data);
    }
    
    /**
     * 获取登录用户粉丝
     * @param int $id          用户id
     * @param int $p           页码
     * @param int $per_page    单页条目数
     * @return json            粉丝id、nickname、gender、avatar_url、relation（是3 否互粉）
     */
    public function getFans($id=0, $p=1, $per_page=12){

        //获取用户单页粉丝
        $condition_in = 'SELECT uid FROM md_follow WHERE pid =' . $id;
        $page = page('User', $per_page, 'id in (' .$condition_in. ')');

        $fans_list = $page['list']; //单页粉丝内容
        $is_fans = M("Follow")->where(array("pid" => $id))->getField("uid, relation");//获取relation是否是互粉，如relation=3为互粉
        if($fans_list != NULL){
            foreach ($fans_list as $key=>$value){
                unset($fans_list[$key]);
                $fans_list[$key]['id'] = $value['id'];
                $fans_list[$key]['nickname'] = $value['nickname'];
                $fans_list[$key]['gender'] = $value['gender'];
                $fans_list[$key]['signature'] = $value['signature']; //个性签名
                $fans_list[$key]['relation'] = $is_fans[$value['id']];      //将是否互粉加进去
                $fans_list[$key]['avatar_url'] = $this->www_url . $value['avatar_url'];
            }
            $data = array(
                "code"  => 1, 
                "msg"   => "用户粉丝读取成功", 
                "data"  => $fans_list,
                'count' => $page['total'],
                );
        }else{
            $data = array(
                "code" => 0, 
                "msg"  => "暂无粉丝");
        }
        $this->ajaxReturn($data);
    }
    
    /**
     * 获取登录用户关注
     * @param int $id          用户id
     * @param int $p           页码
     * @param int $per_page    单页条目数
     * @return json            被关注人id、nickname、gender、avatar_url、relation（是3 否互粉）
     */
    public function getAttentions($id=0, $p=1, $per_page=12){

        //获取用户单页粉丝
        $condition_in = 'SELECT pid FROM md_follow WHERE uid =' . $id;
        $page = page('User', $per_page, 'id in (' .$condition_in. ')');

        $fans_list = $page['list']; //单页粉丝内容
        $is_fans = M("Follow")->where(array("uid" => $id))->getField("pid, relation");//获取relation是否是互粉，如relation=3为互粉
        if($fans_list != NULL){
            foreach ($fans_list as $key=>$value){
                unset($fans_list[$key]);
                $fans_list[$key]['id'] = $value['id'];
                $fans_list[$key]['nickname'] = $value['nickname'];
                $fans_list[$key]['gender'] = $value['gender'];
                $fans_list[$key]['signature'] = $value['signature']; //个性签名
                $fans_list[$key]['relation'] = $is_fans[$value['id']];      //将是否互粉加进去
                $fans_list[$key]['avatar_url'] = $this->www_url . $value['avatar_url'];
            }
            $data = array(
                "code"  => 1, 
                "msg"   => "我的关注数据读取成功", 
                "data"  => $fans_list,
                'count' => $count,
                );
        }else{
            $data = array(
                "code" => 0, 
                "msg"  => "暂无关注"
                );
        }
        $this->ajaxReturn($data);
    }
    
    /**
     * 关注
     * @param type $uid 关注者（登录用户）id
     * @param type $pid 被关注者id
     * @return json code/msg
     */
    public function attention($uid=0, $pid=0){
        if($uid!=0 && $pid!=0){
            $relation = 1;
            //查看用户是否已关注过
            $attented = M('Follow')->where(array('uid' => $uid, 'pid' => $pid))->find();
            if($attented == NULL){
                //查看对方是否关注过我
                if(M('Follow')->where(array('uid' => $pid, 'pid' => $uid))->find()){
                    $relation = 3;
                }
                $data = array(
                    'uid'       => $uid,
                    'pid'       => $pid,
                    'relation'  => $relation,
                );
                if(M('Follow')->add($data)){
                    $tmp = array(
                        'code'  => 1,
                        'msg'   => '成功',
                    );
                } else {
                    $tmp = array(
                        'code' => -1,
                        'msg'  => '失败',
                    );
                }
            }else{
                $tmp = array(
                    'code' => 0,
                    'msg'  => '已关注'
                );
            }
            $this->ajaxReturn($tmp);
        }
    }
    
    /**
     * 取消关注
     * @param int 用户id
     * @param int 被取消关注者id
     */
    public function cancelAttention($uid=0, $pid=0){

        //查看对方是否关注过我
        if(M('Follow')->where(array('uid' => $pid, 'pid' => $uid))->find()){
            M('Follow')->where(array('uid' => $pid, 'pid' => $uid))->save(array('relation' => 1)); //关注过，将互粉改为单向关注
        }
        $data = array(
            'uid'       => $uid,
            'pid'       => $pid,
        );
        if(M('Follow')->where($data)->delete()){
            $tmp = array(
                'code'  => 1,
                'msg'   => '成功',
            );
        } else {
            $tmp = array(
                'code'  => 0,
                'msg'  => '失败',
            );
        }
        $this->ajaxReturn($tmp);
    }
    
    /**
     * 获取模特部分信息，照片数，粉丝数，满意数等，显示在麻豆相册和详情上部信息
     * @param type $id 麻豆id
     */
    public function getModelPartInfo($id){
        
        $model_info = M('User')->where(array('id' => $id))->field('nickname, avatar_url, cover_img')->find();
        if($model_info){
            $model_info['avatar_url'] = $this->www_url . $model_info['avatar_url'];
            $model_info['cover_img']  = $this->www_url . $model_info['cover_img'];
            $model_info['pic_num'] = M('Weibo')->where(array('uid' => $id, 'status' => 1))->join('md_weibo_pic ON md_weibo.id=md_weibo_pic.w_id')->count();
            $model_info['fans_num'] = M('Follow')->where(array('pid' => $id, 'status' => 1))->count();
            $tmp = M('User_model')->field('value, satisfy')->where(array('uid' => $id))->find();
            $model_info['portrait_value'] = $tmp['value'];
            $model_info['satisfy_num'] = $tmp['satisfy_num'];
            $tmp = M('User_model')->field('value')->where(array('uid' => $id))->find();
            $model_info['portrait_value'] = $tmp['value'];
            $this->ajaxReturn(array(
                'code' => 1,
                'msg'  => '模特信息获取成功',
                'data' => $model_info,
            ));
        }else{
            $this->ajaxReturn(array(
                'code' => 0,
                'msg'  => '模特信息获取失败',
            ));
        }
    }

    /**
     * 查看模特详情
     * @param int $id  模特id
     * @param int $uid 用户id
     * @return json code、msg、data：。。。
     */
    public function getModelDetail($id=0, $uid=0){

        $fields = 'city,age,height,weight,bust,waistline,hipline,shoe_size,job,style';
        $basic_info = M('User_model')->field($fields)->where(array('uid' => $id))->find();
        if($basic_info != NULL){
            $city = $this->getCity($basic_info['city']);            
            $basic_info['city'] = $city['city']['name'];
            $parameter = array('style', 'job');
            $config = get_array_configs($parameter);    //从配置中获取style和job的值
            $basic_info['style'] = array('code' => $basic_info['style'], 'msg' => $config['style'][$basic_info['style']]);
            $basic_info['job']  = array('code' => $basic_info['job'], 'msg' => $config['job'][$basic_info['job']]);
            $basic_info['is_attention'] = 0;
            if(M('Follow')->where(array('uid' => $uid, 'pid' => $id))->find()){
                $basic_info['is_attention'] = 1;
            }            
            $data = array(
                'code' => 1,
                'msg'  => '模特详情信息获取成功!',
                'data' => $basic_info,
            );
        }else{
            $data = array(
                'code' => 0, 
                'msg'=>'模特详情获取失败！'
            );
        }
        $this->ajaxReturn($data);
    }
    
    /**
     * 获取模特分类
     */   
    public function getClasses(){

        $cfg = array(
            "star_sign", "hair_style", 
            "hair_color", "face_style", 
            "style", "job"
            );
        $config = get_array_configs($cfg);
        if($config != NULL){
            $data = array(
                "code"  => 1,
                "msg"   => "分类获取成功",
                "data"  => $config,
            );
        }else{
            $data = array(
                "code"  => 0,
                "msg"   => "分类获取失败",                
            );
        }
        $this->ajaxReturn($data);
    }
    
    /**
     * 模特相册下微博列表
     * @param int $uid      用户id
     * @param int $id       模特id
     * @param int $p        页码
     * @param int $per_page 单页条目数
     * @return json 微博列表基本内容
     */
    public function getModelWeibo($uid=0, $id=0, $p=1, $per_page=4, $type=1){

        //获取单页微博列表以及总的条目数
        $where['uid'] = $id;
        if($type == 0){
            $where['status'] = 1;
        }
        $model = page("Weibo", $per_page, $where, array('add_time'=>'desc'));
        $weibo = $model['list'];  //单页微博列表 
        if($weibo != NULL){
            foreach ($weibo as $w => $v){
                $weibo[$w]['add_time'] = date('d-m', $v['add_time']);                
                //获取微博照片
                $pic = M('Weibo_pic')->field('pic_url')->where(array('w_id' => $v['id']))->select();
                if($pic != NULL){
                    foreach ($pic as $k => $p){
                        $pic[$k] = $this->www_url . $p['pic_url'];
                    }
                }
                $weibo[$w]['pic'] = $pic;
                $weibo[$w]['is_favour'] = 0; //用户是否赞过微博，0未赞
                if(M('Favour')->where(array('uid' => $uid, 'fid' => $v['id'], 'type' => 1))->find()){
                    $weibo[$w]['is_favour'] = 1;
                }
            }
            $data = array(
                'code'  => 1,
                'msg'   => '微博列表获取成功',
                'data'  => $weibo,
                'count' => $model['total'],
            );
        }else{
            $data = array(
                'code'  => 0,
                'msg'   => '暂无微博',
            );
        }
        $this->ajaxReturn($data);
    }
    
    /**
     * 查看微博详情
     * @param int $id  微博id
     * @param int $uid 用户id
     * @return json code、msg、data：微博内容、照片、赞数目、用户是否赞过
     */
    public function getWeiboDetail($id=0, $uid=0){

        $detail = M('Weibo')->field('content, favour')->where(array('status' => 1, 'id' => $id))->find();      
        if($detail != NULL){
            $detail['is_favour'] = 0; //用户是否赞过微博，0未赞
            if($uid!=0 && M('Favour')->where(array('uid' => $uid, 'fid' => $id, 'type' => 1))->find()){
                $detail['is_favour'] = 1;
            }
            $detail['comment_num'] = M('Comment')->where(array('weibo_id' => $id, 'type' => 0))->count();
            $detail['favour_num']  = M('Favour')->where(array('fid' => $id, 'type' => 1))->count();
            //获取微博照片
            $pic = M('Weibo_pic')->field('pic_url')->where(array('w_id' => $id))->select();
            foreach ($pic as $k => $v){
                $pic[$k] = $this->www_url . $v['pic_url'];
            }
            $detail['pic'] = $pic;
            $data = array(
                'code'  => 1,
                'msg'   => '模特微博详情获取成功',
                'data'  => $detail,
            );
        } else {
            $data = array(
                'code'  => 0,
                'msg'   => '模特微博详情获取失败',
            );
        }
        $this->ajaxReturn($data);
    }
    
    /**
     * 获取评论页面微博图片及个人信息
     * @param type $uid 用户id
     * @param type $id  微博id
     * @return code、msg、data：。。
     */
    public function getComments($uid=0, $id=0){

        $comment = M('Weibo')->where(array('md_weibo.status' => 1, 'md_user.status'=> 1, 'md_weibo.id' => $id))
                ->field(array('md_user.id' => 'uid', 'nickname', 'avatar_url', 'content', 'favour', 'add_time'))
                ->join('md_user on md_user.id = md_weibo.uid')
                ->find();
        if($comment != NULL){
            //获取该条微博的所有图片
            $pic = M('Weibo_pic')->field('pic_url')->where(array('w_id' => $id))->select();
            foreach ($pic as $key => $value){
                $pic[$key]  = $this->www_url . $value['pic_url'];
            }
            $comment['pic'] = $pic;
            //处理微博添加时间
            $minutes = (int)((time() - $comment['add_time']) / 60);
            if($minutes <= 60){
                $show_time = (int)$minutes . '分钟';
            }else{
                if(($minutes / 60) <= 24){
                    $show_time = (int)($minutes / 60) . '小时';
                }else{
                    $show_time = date('m-d H:i', $comment['add_time']);
                }
            }
            $comment['add_time'] = $show_time;
            
            $is_favour = 0; //是否赞过，0未赞
            if(M('Favour')->where(array('uid' => $uid, 'fid' => $id, 'type' => 1))->find()){
                $is_favour = 1;
            }
            $data = array(
                'code'  => 1,
                'msg'   => '微博发布人信息获取成功',
                'data'  => $comment,
                'is_favour' => $is_favour,
            );
        } else {
            $data = array(
                'code'  => 0,
                'msg'   => '微博发布人信息获取失败',                
            );
        }
        $this->ajaxReturn($data);
    }
    
    /**
     * 获取评论列表
     * @param int $id       微博id
     * @param int $p        页码
     * @param int $per_page 单页条目数
     * @param int $type 0微博评论，1试衣间评论
     * @return json 评论人nickname、头像、评论时间、内容
     */
    public function getCommontsList($id=0, $p=1, $per_page=6, $type=0){
        
        $page = page('Comment', $per_page, array('weibo_id' => $id, 'md_comment.status' => 1, 'md_user.status' => 1, 'type' => $type), 
                array('add_time' => 'desc'), 'md_user on md_comment.uid = md_user.id', 'nickname, avatar_url, content, add_time');
        $comments = $page['list'];
        
        if($comments != NULL){
            foreach ($comments as $k => $v){
                $comments[$k]['add_time'] = date('m-d H:i', $v['add_time']);
                $comments[$k]['avatar_url'] = $this->www_url . $v['avatar_url'];
            }
            $data = array(
                'code'  => 1,
                'msg'   => '评论获取成功',
                'data'  => $comments,
                'count' => $page['total'], //总的评论数
            );
        }  else {
            $data = array(
                'code'  => 0,
                'msg'   => '暂无评论',
            );
        }
        $this->ajaxReturn($data);
    }
    
    /**
     * 评论
     * @param int $wid       微博id
     * @param int $uid        评论人id
     * @param string $content 评论内容
     * @param int $type 0评论微博，1评论试衣间状态
     */
    public function comment($wid=0, $uid=0, $content='', $type=0){
        
        $data = array(
            'uid'       => $uid,
            'weibo_id'  => $wid,
            'content'   => $content,
            'add_time'  => NOW_TIME,
            'type'      => $type,
        );
        if(M('Comment')->add($data)){
            $data = array(
                'code'      => 1,
                'msg'       => '评论成功',
            );            
        }else{
            $data = array(
                'code'  => 0,
                'msg'   => '评论失败，请重试',
            );
        }
        $this->ajaxReturn($data);
    }
    /*
     * M('User')->field('avatar_url')->where(array('id' => $uid))->find();
        $data = array(
            'code'      => 1,
            'msg'       => '评论成功',
            'content'   => $content,
            'time'      => NOW_TIME,
        );  
     */
    
    /**
     * 获取用户编辑信息
     * @param type $id 用户id
     */
    public function getUserInfo($id=0){

        $userinfo = M('User_model')->where(array('uid' => $id))->find();
        if($userinfo != NULL){
            //从数据库中获取以下字段值的枚举值
            $var_cfg = array('star_sign', 'face_style', 'hair_style', 'hair_color', 'style', 'part', 'job');
            $config = get_array_configs($var_cfg);
            foreach ($config as $key => $value){
                $userinfo[$key] = array('id' => $userinfo[$key], 'name' => $value[$userinfo[$key]]);
            }
            $userinfo['city'] = $this->getCity($userinfo['city']);
            unset($userinfo['id']);
            $data = array(
                'code'  => 1,
                'msg'   => '模特信息获取成功',
                'data'  => $userinfo,
            );
        } else {
            $data = array(
                'code'  => 0,
                'msg'   => '模特信息获取失败',
            );
        }
        $this->ajaxReturn($data);
        
    }

    /**
     * 编辑模特信息
     */
    public function editUserInfo(){
        $post = I('post.');
        $id = $post['id'];
        if(isset($id)){
            $nickname = empty($post['nickname']) ? '' : $post['nickname'];
            $data = array(
                'nickname'   => $nickname,              //昵称
                'age'        => empty($post['age']) ? 21 : $post['age'],          //年龄
                'star_sign'  => empty($post['star_sign']) ? 0 : $post['star_sign'],    //星座
                'hobby'      => empty($post['hobby']) ? '' : $post['hobby'],        //爱好
                'bust'       => empty($post['bust']) ? 90 : $post['bust'],         //胸围
                'waistline'  => empty($post['waistline']) ? 60 : $post['waistline'],    //腰围
                'hipline'    => empty($post['hipline']) ? 90 : $post['hipline'],      //臀围
                'height'     => empty($post['height']) ? 168 : $post['height'],       //身高
                'weight'     => empty($post['weight']) ? 48 : $post['weight'],       //体重
                'face_style' => empty($post['face_style']) ? 0 : $post['face_style'],   //脸型
                'shoe_size'  => empty($post['shoe_size']) ? 38 : $post['shoe_size'],    //鞋码
                'hair_style' => empty($post['hair_style']) ? 0 : $post['hair_style'],   //发型
                'hair_color' => empty($post['hair_color']) ? 0 : $post['hair_color'],   //发色
                'style'      => empty($post['style']) ? 0 : $post['style'],        //风格
                'part'       => empty($post['part']) ? 0 : $post['part'],         //满意部位
                'job'        => empty($post['job']) ? 0 : $post['job'],          //职业
                'city'       => empty($post['city']) ? 3 : $post['city'],         //城市
                'value'      => empty($post['value']) ? 100 : $post['value'],        //价位
            );

            if(M('User_model')->where(array('uid' => $id))->save($data)){
                M('User')->save(array('id' => $id, 'nickname' => $nickname));
                $data = array(
                    'code'  => 1,
                    'msg'   => '模特信息编辑成功',
                );
            }else{
                $data = array(
                    'code'  => 0,
                    'msg'   => '模特信息编辑失败',
                );
            }
            $this->ajaxReturn($data);
        }
        $this->ajaxReturn(array(
            'code'  => 0,
            'msg'   => '模特信息编辑失败'
        ));
    }
    
    /**
     * 获取用户信息
     * @param type $id 用户id
     * @return code、msg、data：头像，昵称，背景图、粉丝数、关注数
     */
    public function getMyInfo($id=0){

        $userinfo = M('User')->field('nickname, avatar_url, cover_img')->where(array('id' => $id))->find();
        if($userinfo != NULL){
            if($userinfo['model_card'] != ''){
                $userinfo['model_card'] = $this->www_url . $userinfo['model_card'];
            }
            $userinfo['avatar_url'] = $this->www_url . $userinfo['avatar_url'];
            $userinfo['cover_img'] = $this->www_url . $userinfo['cover_img'];
            $userinfo['fans_num'] = M('Follow')->where(array('pid' => $id, 'status' => 1))->count('id');
            $userinfo['atten_num'] = M('Follow')->where(array('uid' => $id, 'status' => 1))->count('id');
            $data = array(
                'code'  => 1,
                'msg'   => '用户信息获取成功',
                'data'  => $userinfo,
            );
        } else {
            $data = array(
                'code'  => 0,
                'msg'   => '用户信息获取失败',
            );
        }
        $this->ajaxReturn($data);
    }
    
    /**
     * 投诉
     * @param int    $cid     投诉人id
     * @param int    $wid     被投诉微博id
     * @param int    $cause   投诉原因
     * @param string $content 投诉内容
     */
    public function complaint($cid=0, $wid=0, $cause, $content){

        $weibo = M('Weibo')->field('uid')->where(array('id' => $wid))->find();
        $data = array(
            'cid'  => $cid,
            'wid'  => $wid,
            'bid'  => $weibo['uid'],
            'cause'=> $cause,
            'content' => $content,
        );
        if(M('Complaint')->add($data)){
            $return = array(
                'code' => 1,
                'msg'  => '投诉成功，等待处理',
            );
        }else{
            $return = array(
                'code' => 0,
                'msg'  => '投诉失败',
            );
        }
        $this->ajaxReturn($return);
    }
    
    /**
     * 获取城市信息
     * @param type 地区id
     * @return type
     */
    public function getCity($districtid){
        $district = M('City')->where(array('id' => $districtid))->find();
        $city = M('City')->where(array('id' => $district['pid']))->find();
        $provience = M('City')->where(array('id' => $city['pid']))->find();
        return array(
            'provience' => array('id' => $provience['id'], 'name' => $provience['name']),
            'city' => array('id' => $city['id'], 'name' => $city['name']),
            'district'  => array('id' => $district['id'], 'name' => $district['name']),
        );
    }
    
    /**
     * 获取所在地信息，即省市区
     * @param type $province_id 省id
     * @param type $city_id     市id
     * @param type $county_id   区县id
     */
    public function getLocation($province_id, $city_id, $county_id){
        
    }
    
    /**
     * 赞
     * @param type $type 赞类型 0模特，1微博，2试衣间
     * @param type $uid  赞用户id
     * @param type $id   被赞模特/微博/试衣间id
     */
    public function favour($type=0, $uid=0, $id=0){

        if(M('Favour')->where(array('uid' => $uid, 'fid' => $id, 'type' => $type))->find()){
            $data = array(
                'code' => 0,
                'msg'  => '已赞',
            );
        }else{
            M('Favour')->add(array('uid' => $uid, 'fid' => $id, 'type' => $type, 'createtime' => NOW_TIME));
            $data = array(
                'code' => 1,
                'msg'  => '赞成功',
            );
        }
        $this->ajaxReturn($data);
    }
    
    /**
     * 取消赞
     * @param int $type 类型：0模特，1微博，2试衣间
     * @param int $uid  取消赞用户id
     * @param int $id   被取消赞用户id
     */
    public function cancelFavour($type=0, $uid=0, $id=0){

        if(M('Favour')->where(array('uid' => $uid, 'fid' => $id, 'type' => $type))->delete()){
            $data = array(
                'code' => 1,
                'msg'  => '取消成功',
            );
        }else{
            $data = array(
                'code' => 0,
                'msg'  => '失败，不存在记录',
            );
        }
        $this->ajaxReturn($data);
    }

    /**
     * 用户反馈
     */
    public function feedback($uid=0, $content=''){

        $data = array(
            'uid'           => $uid,
            'content'       => $content,
            'create_time'   => NOW_TIME,
        );
        if(M('Feedback')->add($data)){
            $data = array(
                'code'  => 1,
                'msg'   => '反馈成功',
            );
        }else{
            $data = array(
                'code'  => 0,
                'msg'   => '反馈失败',
            );
        }
        $this->ajaxReturn($data);
    }
    
    /**
     * 获取系统消息
     * @param type $id 用户id
     */
    public function getSysMsg($id=0){
        $data = M('Message')->field('id,content,createtime,status,subtype')->where(array('uid' => $id, 'type' => 1))->order('createtime desc')->select();
        if($data != NULL){
            $new_num = M('Message')->where(array('uid' => $id, 'type' => 1, 'status' => 0))->count();
            $this->ajaxReturn(array(
                'code'  => $new_num,
                'msg'   => '获取成功',
                'data'  => $data,
                'new_num' => $new_num,
            ));
        }else{
            $this->ajaxReturn(array(
                'code' => -1,
                'msg'  => '暂无消息',
            ));
        }
    }
    
    /**
     * 读消息，读过消息status=1
     * @param int $id 用户id
     */
    public function read($id=0){
        if(M('Message')->where(array('uid' => $id))->save(array('status' => 1))){
            $this->ajaxReturn(array(
                'code'  => 1,
                'msg'   => '已读取',
            ));
        }else{
            $this->ajaxReturn(array(
                'code'  => 0,
                'msg'   => '读取失败',
            ));
        }
    }
    
    /**
     * 获取模特试衣间列表
     * @param int $uid 用户uid
     */
    public function getDressList($uid=0, $p=1, $per_page=3){

        $dressing = page('Invite_experience', $per_page, array('is_recommend' => 2), array('id' => 'desc'), '', 'id, mid, product_url, reason, recommend_time');
        $data = $dressing['list'];
        if(is_array($data)){
            foreach ($data as $key => $value){
                //获取并处理微博图片
                $pics = M('Experience_pics')->field('pic_url')->where(array('invite_id' => $value['id']))->select();
                foreach ($pics as $k => $pic){
                    $pics[$k] = $this->www_url . $pic['pic_url'];
                }
                $data[$key]['pic'] = $pics;
                //获取模特信息，昵称、头像
                $model = M('User')->field('avatar_url,nickname')->where(array('id' => $value['mid']))->find();
                $model['avatar_url'] = $this->www_url . $model['avatar_url'];
                $data[$key]['model'] = $model;
                //用户是否关注模特
                $is_attention = 0;
                if(M('follow')->where(array('uid' => $uid, 'pid' => $value['mid']))->find()){
                    $is_attention = 1;
                }
                $data[$key]['is_attention'] = $is_attention;
                //用户是否赞过该分享
                $is_favour = 0;
                if(is_array(M('Favour')->where(array('uid' => $uid, 'fid' => $value['id'], 'status' => 1, 'type' => 2))->find())){
                    $is_favour = 1;
                }
                $data[$key]['is_favour'] = $is_favour;
                //该分享被赞的数目
                $favour = M('Favour')->where(array('fid' => $value['id'], 'status' => 1, 'type' => 2))->count();
                $data[$key]['favour'] = $favour;
                //评论数
                $comment_num = M('Comment')->where(array('weibo_id' => $value['id'], 'type' => 1, 'status' => 1))->count();
                $data[$key]['comment_num'] = $comment_num;

                //处理微博添加时间
                $minutes = (int)((time() - $value['recommend_time']) / 60);
                if($minutes <= 60){
                    $show_time = (int)$minutes . '分钟';
                }else{
                    if(($minutes / 60) <= 24){
                        $show_time = (int)($minutes / 60) . '小时';
                    }else{
                        $show_time = date('m-d H:i', $value['recommend_time']);
                    }
                }
                $data[$key]['recommend_time'] = $show_time;
                }
            $this->ajaxReturn(array(
                'code'  => 1,
                'msg'   => '试衣间列表获取',
                'data'  => $data,
                'count' => $dressing['total'],
            ));
        }
        $this->ajaxReturn(array(
            'code'  => 0,
            'msg'   => '暂无体验',
        ));
    }
}